import 'package:flutter/material.dart';

extension DoubleFit on double {
  double get px {
    return ScreenAdapterUtil.setPx(this);
  }

  double get rpx {
    return ScreenAdapterUtil.setRpx(this);
  }
}

extension IntFit on int {
  double get px {
    return ScreenAdapterUtil.setPx(this.toDouble());
  }

  double get rpx {
    return ScreenAdapterUtil.setRpx(this.toDouble());
  }
}

///
/// Text 组件中设置 this.textScaler = TextScaler.noScaling,
class ScreenAdapterUtil {
  // 1.基本信息
  static double screenWidth = 0;
  static double screenHeight = 0;
  static double dpr = 0;
  static double statusHeight = 0;

  static double rpx = 0;
  static double px = 0;
// 这里设置standardSize 为可选属性，如果使用者愿意以非iPhone6为标准，那么你使用时候传入对应的标准即可。这里默认750
  static void initialize(BuildContext context, {double standardSize = 750}) {
    final mediaQueryData = MediaQuery.of(context);
    screenWidth = mediaQueryData.size.width;
    screenHeight = mediaQueryData.size.height;

    // 5.计算rpx px的大小, iPhone6下 1px = 2rpx
    rpx = screenWidth / standardSize;
    px = screenWidth / standardSize;
    print('rpx ---- $rpx  ');
  }

  static double setRpx(double size) {
    return rpx * size;
  }

  static double setPx(double size) {
    return px * size;
  }
}
